package com.treasure.mall.zbqmallapi.controller.life;

import com.treasure.mall.biz.dto.life.ShopSearchCondition;
import com.treasure.mall.biz.entity.life.ShopPO;
import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.biz.service.core.life.LifeShopService;
import com.treasure.mall.biz.vo.PageDataVO;
import com.treasure.mall.zbq.common.annotation.CurrentUser;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.base.ResultCodeEnum;
import com.treasure.mall.zbqmallapi.annotation.AccessControl;
import com.treasure.mall.zbqmallapi.mapstruct.ShopMapStruct;
import com.treasure.mall.zbqmallapi.vo.life.ShopDetailsVO;
import com.treasure.mall.zbqmallapi.vo.life.ShopVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 
 * <p>
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.zbqmallapi.controller.life
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/10/15 11:37 上午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 *
 */
@RestController
@RequestMapping("/life/shop")
public class ShopController {

    @Autowired
    private LifeShopService shopService;

    /**
     * 获取门店列表
     *
     * @param condition
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public ApiResult<PageDataVO<ShopVO>> list(ShopSearchCondition condition, @RequestParam(required = false, defaultValue = "1") int page,
                                              @RequestParam(required = false, defaultValue = "20") int size) {
        condition.setDisabled(false);
        Page<ShopPO> pages = shopService.findAll(page, size, condition);
        PageDataVO<ShopVO> pageDataVO = new PageDataVO<>();
        pageDataVO.setPageCount(pages.getTotalPages());
        pageDataVO.setPageIndex(page);
        pageDataVO.setPageSize(size);
        pageDataVO.setTotal(pages.getTotalElements());
        List<ShopDetailsVO> list = ShopMapStruct.Instance.pos2vos(pages.getContent());
        pageDataVO.setRows(ShopMapStruct.Instance.detailsVos2shopVos(list));
        return ApiResult.ok(pageDataVO);
    }

    /**
     * 店铺详情
     *
     * @param shopId
     * @return
     */
    @GetMapping("/details/{shopId}")
    public ApiResult details(@PathVariable Long shopId) {
        ShopPO shopPO = shopService.findById(shopId);
        if (shopPO == null) {
            return ApiResult.error(ResultCodeEnum.ALERT_FAILED, "店铺不存在");
        }
        return ApiResult.ok(ShopMapStruct.Instance.po2detailsVo(shopPO));
    }


    /**
     * 添加预约记录
     *
     * @param user
     * @param shopId
     * @return
     */
    @GetMapping("/record/insert/{shopId}")
    @AccessControl(needLogin = true)
    public ApiResult insertRecord(@CurrentUser UserPO user, @PathVariable Long shopId) {
        shopService.insertRecord(user.getUserId(), shopId);
        return ApiResult.ok();
    }

    /**
     * 获取店铺城市
     *
     * @param type type 0 掌眼专区城市  1修复专区城市
     * @return
     */
    @GetMapping("/cities")
    public ApiResult cities(@RequestParam(required = false, defaultValue = "0") int type) {
        return ApiResult.ok(shopService.findCity(type));
    }

}
